import hmac
import  hashlib
#from MMMM.BuildIndex import Ir,Ic,I,s1





prime = 148300296503541537023967543385571281566188269121696986355794642903163978311488366178620508621203382790644644256351530344017086776119734526929467544594283880389370982620148075975876610977802799762624795305966277480303398775064011730140292446255046328316204433780624821532022148316432675132662755400581378870583
key_s = b'\x1a\xde\x9b\xda\xadNq\xf4\xa5\x04g\x02\x99E\x8d\xcf[\x0f\xa6Dx\xe7\t\xbb\x1e\xb2n\xbd|h\xcf\x16'
key_m_1 = b'\xba\x8a\xed\x17y\x8f2[y]{\xd7\x1a\xb8\x05\x08\xf2CL\xe8\xdaY\xa2\xd0o=\x87\xc5De\xb1\x95'
key_m_2= '8bjp6lyrgfn370i5'
key_1 = b'\xaa%\x00"\x08)+\x84\xf1I\x0e\xeb\xb6\xb2\xd4L\x8eBd\xbc{\xc1\xc7\xc9\xd3`\xb1\xfb\xbbZ\xa2\xed'
iv = 'gvmr10z4pc6xqn3l'
iv_bytes = bytes(iv,encoding='utf8')

def exgcd(a, b):
    if b == 0:
        return 1, 0, a
    else:
        x, y, q = exgcd(b, a % b)
        x, y = y, (x - (a // b) * y)
        return x, y, q

# 扩展欧几里得求逆元
def ModReverse(a,p):
    x, y, q = exgcd(a,p)
    if q != 1:
        raise Exception("No solution.")
    else:
        return (x + p) % p #防止负数

#获取column，column_msk
def get_column(prime,T,Ir):
    print("123456")
    column_msk = []
    column = []
    for i in range(0,len(T)):
        for j in range(1,len(Ir)):
            e = (T[i][1] * Ir[j]) % prime

            m = (T[i][2] * ModReverse(e,prime)) % prime
            mac =hmac.new(key_s,bytes(str(e^m^ModReverse(Ir[j],prime)),encoding='utf-8'),digestmod=hashlib.sha256).hexdigest()
            mac = int(mac.upper(),16)
            # print(mac)
            if mac ==T[i][0]:
                column.append(j)
                column_msk.append(m)

    return column,column_msk
'''
# 获取encrypted_file文件指针，key_s为哈希链的密钥
def get_encryptedfile(column,key_s,column_msk,T):
    encrypted_file = []
    if len(T)!= len(column):
        print('no result')
    else:
        for i in range(1,len(Ic)):
            if len(column) == 0:
                print('no result')
                break
            else:
                tag_1 = [1] * s1 
                tag_2 = [0] * s1 
                for j in range(0,len(column)-1):
                    for m in range(0,s1):
                        if tag_1[m] == 1:
                            for n in range(0,s1):
                                front = I[i][column[j]][m]-column_msk[j]
                                mac_front = I[i][column[j + 1]][n] - column_msk[j + 1]
                                value = hmac.new(key_s,str(front).encode('utf-8'),digestmod=hashlib.sha256).hexdigest()
                                if int(value.upper(),16) == mac_front:
                                    tag_2[n] = 1
                                    break
                                else:
                                    continue

                        else:
                            continue
                    tag_1 = tag_2.copy()
                    tag_2 = [0] * s1

                mark = 0
                for tag in tag_1:
                    if tag == 1:
                        mark = 1
                        break
                    else:
                        continue

                if mark == 1:
                    encrypted_file.append([Ic[i]])
    return encrypted_file
'''
